package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.db.dao.LitemallUserBalanceLogMapper;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.dao.LitemallWithdrawMapper;
import org.linlinjava.litemall.db.dao.UserStatisticsMapper;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LitemallUserBalanceLog;
import org.linlinjava.litemall.db.domain.LitemallWithdraw;
import org.linlinjava.litemall.db.domain.LitemallWithdrawExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallWithdrawService {
    @Resource
    private LitemallWithdrawMapper litemallWithdrawMapper;

    @Resource
    private LitemallUserMapper litemallUserMapper;

    @Resource
    private UserStatisticsMapper userStatisticsMapper;

    @Resource
    private LitemallUserBalanceLogMapper userBalanceLogMapper;

    public void saveWithdraw(LitemallWithdraw withdraw) {
        litemallWithdrawMapper.insert(withdraw);
    }

    public List<LitemallWithdraw> queryList(Integer userId,Integer page, Integer limit) {
        return queryList(userId, page, limit, "add_time", "desc");
    }

    public List<LitemallWithdraw> queryAdminList(String username,Integer userId, Byte status, Integer page, Integer limit, String sort, String order) {
        LitemallWithdrawExample example = new LitemallWithdrawExample();
        LitemallWithdrawExample.Criteria criteria = example.or();
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status!=null){
            criteria.andStatusEqualTo(status);
        }
        if(StringUtils.isNotBlank(username)){
            criteria.andWxAccountLike("%" + username + "%" );
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        return litemallWithdrawMapper.selectByExample(example);
    }

    public List<LitemallWithdraw> queryList(Integer userId, Integer page, Integer limit, String sort, String order) {
        LitemallWithdrawExample example = new LitemallWithdrawExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        example.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        return litemallWithdrawMapper.selectByExample(example);
    }

    public LitemallWithdraw findById(Integer id) {
        return litemallWithdrawMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public int updateByStatus(String userName,LitemallWithdraw user) {

        LitemallWithdraw ord = litemallWithdrawMapper.selectByPrimaryKey(user.getId());

        Integer userId = user.getUserId();
        LitemallUser litemallUser = litemallUserMapper.selectByPrimaryKey(userId);

        if(ord.getPrice().compareTo(litemallUser.getBalance()) > 0 ){
            return -1;
        }

        //更新提现状态
        LitemallWithdraw tmp = new LitemallWithdraw();
        tmp.setId(user.getId());
        tmp.setUpdateTime(LocalDateTime.now());
        tmp.setStatus(user.getStatus());
        litemallWithdrawMapper.updateByPrimaryKey(tmp);

        //修改用户余额
        userStatisticsMapper.updateBalance(userId, ord.getPrice());

        //查询最近余额
        LitemallUser newLitemallUser = litemallUserMapper.selectByPrimaryKey(userId);

        //记录日志
        LitemallUserBalanceLog log = new LitemallUserBalanceLog();
        log.setAddTime(LocalDateTime.now());
        log.setDeleted(false);
        log.setUserId(user.getUserId());
        log.setType(3);//提现
        log.setBeforeBalance(litemallUser.getBalance());
        log.setAfterBalance(newLitemallUser.getBalance());
        log.setUsername(userName);
        userBalanceLogMapper.insert(log);
        return 0;
    }
}
